﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ScreenRemote
{
    public partial class frmMain : Form
    {
        private Screen capSrc = Screen.AllScreens[0];
        private bool running = true;

        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            char[] tr = {'\\','/','.'};
            foreach (Screen screen in Screen.AllScreens)
            {
                this.cmbScreen.Items.Add(screen.DeviceName.Trim(tr));
            }

            this.cmbScreen.SelectedIndex = 0;

            this.tmrCapture.Start();
        }

        private Bitmap CaptureScreen(Screen sc)
        {
            Size s = sc.Bounds.Size;
            int xstart = sc.Bounds.Left;
            Bitmap bmp = new Bitmap(s.Width, s.Height);
            Graphics g = Graphics.FromImage(bmp);

            g.CopyFromScreen(xstart, 0, 0, 0, s);

            return bmp;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.pcbScreen.Image = this.CaptureDesktopWithCursor();
            this.statusLabelOne.Text = "ScreenRemote v0.1     Sample rate : " + (1000 / this.tmrCapture.Interval).ToString() + " images/sec";
        }

        private void cmbScreen_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.capSrc = Screen.AllScreens[this.cmbScreen.SelectedIndex];
        }

        static Bitmap CaptureCursor(ref int x, ref int y)
        {
            Bitmap bmp;
            IntPtr hicon;
            Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
            Win32Stuff.ICONINFO icInfo;
            ci.cbSize = Marshal.SizeOf(ci);
            if (Win32Stuff.GetCursorInfo(out ci))
            {
                if (ci.flags == Win32Stuff.CURSOR_SHOWING)
                {
                    hicon = Win32Stuff.CopyIcon(ci.hCursor);
                    if (Win32Stuff.GetIconInfo(hicon, out icInfo))
                    {
                        x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
                        y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);
                        Icon ic = Icon.FromHandle(hicon);
                        bmp = ic.ToBitmap();

                        return bmp;
                    }
                }
            }
            return null;
        }

        public Bitmap CaptureDesktopWithCursor()
        {
            int cursorX = 0;
            int cursorY = 0;
            Bitmap desktopBMP;
            Bitmap cursorBMP;
            Graphics g;
            Rectangle r;
            desktopBMP = this.CaptureScreen(this.capSrc);
            if (Cursor.Position.X > this.capSrc.Bounds.Left + this.capSrc.Bounds.Width || Cursor.Position.X < this.capSrc.Bounds.Left)
            {
                mosLabel.Text = "Cursor is not on this Screen";
                return desktopBMP;
            }
            else
            {
                mosLabel.Text = "Cursor is on this Screen";
                cursorBMP = CaptureCursor(ref cursorX, ref cursorY);
                if (this.capSrc.Bounds.Left < 0)
                {
                    cursorX += this.capSrc.Bounds.Width;
                }

                if(this.capSrc.Bounds.Left > 0)
                {
                    cursorX -= this.capSrc.Bounds.Width;
                }

                if (desktopBMP != null)
                {
                    if (cursorBMP != null)
                    {
                        r = new Rectangle(cursorX, cursorY,
                                cursorBMP.Width, cursorBMP.Height);
                        g = Graphics.FromImage(desktopBMP);
                        g.DrawImage(cursorBMP, r);
                        g.Flush();
                        return desktopBMP;
                    }
                    else
                        return desktopBMP;
                }
            }
            return null;
        }

        private void saveFile()
        {
            this.saveDlg.ShowDialog();
            
            if (this.saveDlg.FileName != string.Empty)
            {
                this.pcbScreen.Image.Save(this.saveDlg.FileName);
            }
        }

        private void captureImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.saveFile();
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            this.saveFile();
        }

        private void btnToggleCap_Click(object sender, EventArgs e)
        {
            if (this.running)
            {
                this.tmrCapture.Stop();
                this.running = false;
                this.btnToggleCap.Image = ScreenRemote.Properties.Resources.startHS;
                this.btnToggleCap.ToolTipText = "Start Capturing";
            }
            else
            {
                this.tmrCapture.Start();
                this.running = true;
                this.btnToggleCap.Image = ScreenRemote.Properties.Resources.StopHS;
                this.btnToggleCap.ToolTipText = "Stop Capturing";
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAbout about = new FrmAbout();
            about.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmOptions options = new FrmOptions(this.tmrCapture.Interval, new OptDelegate(this.OptBack));
            options.ShowDialog();
        }

        private void OptBack(int intervall)
        {
            bool wasrunning = false;

            if (this.running)
            {
                this.tmrCapture.Stop();
                wasrunning = true;
            }

            this.tmrCapture.Interval = intervall;

            if (wasrunning)
            {
                this.tmrCapture.Start();
            }
        }
    }
}
